package org.groupg.practice.guiPractice.tools;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SwingComponentsExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Swing 组件示例");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(600, 400);

            JTabbedPane tabbedPane = new JTabbedPane();

            // 添加不同的标签页展示各种组件
            tabbedPane.addTab("基本组件", createBasicComponentsPanel());
            tabbedPane.addTab("布局管理器", createLayoutManagerPanel());
            tabbedPane.addTab("事件处理", createEventHandlingPanel());

            frame.add(tabbedPane);
            frame.setVisible(true);
        });
    }

    private static JPanel createBasicComponentsPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 2, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 按钮
        JButton button = new JButton("点击我");
        button.addActionListener(e -> JOptionPane.showMessageDialog(null, "按钮被点击了!"));

        // 标签
        JLabel label = new JLabel("这是一个标签");

        // 文本框
        JTextField textField = new JTextField(15);
        textField.setToolTipText("请输入文本");

        // 密码框
        JPasswordField passwordField = new JPasswordField(15);

        // 文本区域
        JTextArea textArea = new JTextArea(5, 20);
        textArea.setLineWrap(true);
        JScrollPane scrollPane = new JScrollPane(textArea);

        // 复选框
        JCheckBox checkBox = new JCheckBox("选择我");

        // 单选按钮
        JRadioButton radio1 = new JRadioButton("选项1");
        JRadioButton radio2 = new JRadioButton("选项2");
        ButtonGroup radioGroup = new ButtonGroup();
        radioGroup.add(radio1);
        radioGroup.add(radio2);

        // 下拉列表
        String[] options = {"选项1", "选项2", "选项3"};
        JComboBox<String> comboBox = new JComboBox<>(options);

        // 添加到面板
        panel.add(new JLabel("按钮:"));
        panel.add(button);
        panel.add(new JLabel("标签:"));
        panel.add(label);
        panel.add(new JLabel("文本框:"));
        panel.add(textField);
        panel.add(new JLabel("密码框:"));
        panel.add(passwordField);
        panel.add(new JLabel("文本区域:"));
        panel.add(scrollPane);
        panel.add(new JLabel("复选框:"));
        panel.add(checkBox);
        panel.add(new JLabel("单选按钮:"));
        panel.add(radio1);
        panel.add(new JLabel(""));
        panel.add(radio2);
        panel.add(new JLabel("下拉列表:"));
        panel.add(comboBox);

        return panel;
    }

    private static JPanel createLayoutManagerPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        // 流式布局示例
        JPanel flowPanel = new JPanel(new FlowLayout());
        flowPanel.add(new JButton("流式布局按钮1"));
        flowPanel.add(new JButton("按钮2"));
        flowPanel.add(new JButton("较长的按钮3"));

        // 边界布局示例
        JPanel borderPanel = new JPanel(new BorderLayout());
        borderPanel.add(new JButton("北"), BorderLayout.NORTH);
        borderPanel.add(new JButton("南"), BorderLayout.SOUTH);
        borderPanel.add(new JButton("中"), BorderLayout.CENTER);
        borderPanel.add(new JButton("东"), BorderLayout.EAST);
        borderPanel.add(new JButton("西"), BorderLayout.WEST);

        panel.add(new JLabel("流式布局:"));
        panel.add(flowPanel);
        panel.add(Box.createRigidArea(new Dimension(0, 10)));
        panel.add(new JLabel("边界布局:"));
        panel.add(borderPanel);

        return panel;
    }

    private static JPanel createEventHandlingPanel() {
        JPanel panel = new JPanel(new GridLayout(3, 1, 10, 10));

        // 鼠标事件
        JButton mouseButton = new JButton("鼠标事件测试");
        mouseButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                mouseButton.setText("鼠标进入");
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                mouseButton.setText("鼠标离开");
            }
        });

        // 键盘事件
        JTextField keyField = new JTextField("按键盘试试");
        keyField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                keyField.setText("按下: " + evt.getKeyChar());
            }
        });

        // 焦点事件
        JTextField focusField = new JTextField("点击获得焦点");
        focusField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                focusField.setText("获得焦点");
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                focusField.setText("失去焦点");
            }
        });

        panel.add(mouseButton);
        panel.add(keyField);
        panel.add(focusField);

        return panel;
    }
}
